public class demo3 {
    public class ListNode { int val;
     ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode newlist=new ListNode();
        while(list1!=null&&list1!=null){
            if(list1.val<=list2.val){
                newlist=list1;
                newlist=newlist.next;
                list1=list1.next;
            }
            else{
                newlist=list2;
                newlist=newlist.next;
                list2=list2.next;
            }
        }
        while(list1!=null){
            newlist=list1;
            newlist=newlist.next;
            list1=list1.next;
        }
        while(list2!=null){
            newlist=list2;
            newlist=newlist.next;
            list2=list2.next;
        }
        return newlist;
    }
}
